/**
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 * BaseFlowRoleInsService
 * 流程角色实例
 * 修改纪录
 * 2021-01-11 版本：1.0 wanghanwei 创建。
 * 
 * @version 版本：1.0
 * @author 作者：wanghanwei
 * 创建日期：2021-01-11
 */
package com.topscomm.base.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.topscomm.base.pojo.BaseFlowRoleEntity;
import com.topscomm.base.pojo.BaseFlowRoleInsEntity;
import com.topscomm.base.service.IBaseFlowRoleInsService;
import com.topscomm.base.service.impl.auto.BaseFlowRoleInsServiceAuto;
import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.exception.MessageException;
import com.topscomm.basic.util.PrimaryKeyUtil;
import com.topscomm.cbo.pojo.CboDeptEntity;
import com.topscomm.cbo.pojo.CboUserEntity;
import com.topscomm.cbo.service.impl.CboDeptService;
import com.topscomm.cbo.service.impl.CboUserService;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.vo.ResponseResult;

@Service(value = "baseFlowRoleInsService")
@Scope(value = "singleton")
public class BaseFlowRoleInsService extends BaseFlowRoleInsServiceAuto implements IBaseFlowRoleInsService {
	@Autowired
	private BaseFlowRoleService baseFlowRoleService;
	@Autowired
	private CboUserService cboUserService;
	@Autowired
	private CboDeptService cboDeptService;

	/**
	 * @description:批量保存
	 * @auther: wanghanwei
	 * @date: 2021年1月12日下午2:42:33
	 * @modify:
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public ResponseResult<Object> saveBatch(Map<String, Object> paraMap) {
		paraMap = BaseUtil.decodeSecureMap(paraMap);
		String flowid = ConvertUtil.convertToString(paraMap.get("flowid"));
		String flowinsid = ConvertUtil.convertToString(paraMap.get("flowinsid"));
		if (StringUtils.isEmpty(flowid) || StringUtils.isEmpty(flowinsid)) {
			return ResponseResult.error("flowid或flowinsid 为空");
		}
		// 保存到具体的实例表上 采用先删除再插入
		List<Map<String, Object>> dataList = (List<Map<String, Object>>) paraMap.get("dataList");
		List<BasicEntity> baseFlowRoleInsEntityList = new ArrayList<BasicEntity>();
		List<String> roleIdList = new ArrayList<>();
		// 遍历流程角色
		for (Map<String, Object> baseFlowRole : dataList) {
			List<String> userCodeList = (List<String>) baseFlowRole.get("rolelnslist");
			if (userCodeList.isEmpty()) {
				continue;
			}
			List<Map<String, Object>> userList = (List<Map<String, Object>>) baseFlowRole.get("roleuserlist");
			if (userList.size() == 0) {
				throw new MessageException("角色【" + baseFlowRole.get("name") + "】下不存在人员");
			}
			BaseFlowRoleEntity baseFlowRoleEntity = baseFlowRoleService.queryObjectByWhere("id = '" + userList.get(0).get("roleid") + "'");
			roleIdList.add(baseFlowRoleEntity.getId() + "");
			for (String userCode : userCodeList) {
				CboUserEntity cboUserEntity = cboUserService.queryObjectByWhere("code = '" + userCode + "'");
				if (cboUserEntity == null) {
					throw new MessageException("用户编号【" + userCode + "】不存在");
				}
				CboDeptEntity cboDeptEntity = cboDeptService.queryById(String.valueOf(cboUserEntity.getDeptid()));
				BaseFlowRoleInsEntity baseFlowRoleInsEntity = new BaseFlowRoleInsEntity();
				long[] arrayId = PrimaryKeyUtil.GeneIDs(baseFlowRoleInsEntity.getTableName(), userCodeList.size());
				baseFlowRoleInsEntity.setId(arrayId[0]);
				baseFlowRoleInsEntity.setRolecode(ConvertUtil.convertToString(userList.get(0).get("code")));
				baseFlowRoleInsEntity.setRoleid(Long.valueOf(ConvertUtil.convertToString(userList.get(0).get("roleid"))));
				baseFlowRoleInsEntity.setRolename(baseFlowRoleEntity.getName());
				baseFlowRoleInsEntity.setFlowid(Long.valueOf(flowid));
				baseFlowRoleInsEntity.setFlowinsid(Long.valueOf(flowinsid));
				baseFlowRoleInsEntity.setUserid(cboUserEntity.getId());
				baseFlowRoleInsEntity.setUsercode(cboUserEntity.getCode());
				baseFlowRoleInsEntity.setUsername(cboUserEntity.getName());
				baseFlowRoleInsEntity.setDeptcode(cboDeptEntity.getCode());
				baseFlowRoleInsEntity.setDeptname(cboDeptEntity.getName());
				baseFlowRoleInsEntity.setRelationtype(baseFlowRoleEntity.getRelationtype());
				baseFlowRoleInsEntity.setHandleusercode(this.getSessionUserBean().getUser().getCode());
				baseFlowRoleInsEntity.setHandleusername(this.getSessionUserBean().getUser().getName());
				baseFlowRoleInsEntity.setHandletime(new Date());
				baseFlowRoleInsEntity.setCreateon(new Date());
				baseFlowRoleInsEntity.setModifiedon(new Date());
				baseFlowRoleInsEntityList.add(baseFlowRoleInsEntity);
			}
		}
		if (roleIdList.size() == 0) {
			this.deleteByWhere("flowinsid='" + flowinsid + "'");
		} else {
			this.deleteByWhere("flowinsid='" + flowinsid + "' and roleid in (" + String.join(",", roleIdList) + ")");
		}
		this.insertBatch(baseFlowRoleInsEntityList);
		return ResponseResult.ok("操作成功！");
	}
}
